using System;
using System.Data;
using System.Web;

/// <summary>
/// Summary description for ControlHelper
/// </summary>
public class ControlHelper : System.Web.UI.Page
{

    #region Private
    SqlServerProvider sqlServerProvider;
    #endregion
    
    #region Constructor 
    public ControlHelper()
    {
        sqlServerProvider = new SqlServerProvider();
    }
    public ControlHelper(string connectionString)
    {

        sqlServerProvider = new SqlServerProvider(connectionString);
    }
    #endregion

    #region Properties
    private string commandText = "";
    public string CommandText
    {
        set { commandText = value; }
        get { return commandText; }
    }

    private CommandType commandType = CommandType.Text ;
    public CommandType CommandType
    {
        set { commandType = value; }
        get { return commandType; }
    }

    private string[] parameterCollection;
    public string[] ParameterCollection
    {
        set { parameterCollection = value; }
        get { return parameterCollection; }
    }

    private string[] valueCollection;
    public string[] ValueCollection
    {
        set { valueCollection = value; }
        get { return valueCollection; }
    }

    public void InitProperties()
    {
        sqlServerProvider.CommandText = commandText;
        sqlServerProvider.CommandType = commandType;
        sqlServerProvider.ParameterCollection = parameterCollection;
        sqlServerProvider.ValueCollection = valueCollection;
    }
    
    public int GetTotalRecords()
    {
        int totalRecord = 0;
        object obj = sqlServerProvider.ExecuteScalar();
        if (obj != null) totalRecord = Convert.ToInt32(obj);
        return totalRecord;
    }
    
    public DataTable GetDataTableFromDatabase()
    {
        return sqlServerProvider.GetDataTable();
    }
    public DataTable GetDataTableFromCache(string cacheName)
    {
        DataTable dataTable;
        if(Cache[cacheName] != null)
            dataTable = (DataTable)Cache[cacheName];
        else
            {        
                dataTable = sqlServerProvider.GetDataTable();
                Cache[cacheName] = dataTable;
            }
        return dataTable;
    }   
    #endregion
    
    #region Destructor
    ~ControlHelper()
    {
        sqlServerProvider.CloseConnection();
    }
    #endregion
    
}
